package com.xyh.cloudorder.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 
 * @author liwenquan
 *
 */
public class Convert {
	
	/**
	 * BigDecimal类型转换：转不了，返回null
	 */
	public static BigDecimal toBigDecimal(String str) {
		BigDecimal bd = null;
		try {
			if (Nulls.isNotEmpty(str)) {
				bd = new BigDecimal(str);
			}
		} catch (NumberFormatException e) {
		}
		return bd;
	}
	
	public static Long toLong(String str) {
		Long i = null;
		try {
			if (Nulls.isNotEmpty(str)) {
				i = Long.valueOf(str);
			}
		} catch (NumberFormatException e) {
		}
		return i;
	}
	/**
	 * Short类型转换
	 */
	public static Short toShort(String str) {
		Short st = null;
		if (null != str && !"".equals(str)) {
			st = Short.valueOf(str);
		}
		return st;
	}

	/**
	 * Integer类型转换
	 */
	public static Integer toInteger(String str) {
		Integer i = null;
		if (null != str && !"".equals(str)) {
			i = Integer.valueOf(str);
		}
		return i;
	}

	/**
	 * Float类型转换
	 */
	public static Float toFloat(String str) {
		Float i = null;
		if (null != str && !"".equals(str)) {
			i = new Float(str);
		}
		return i;
	}

	/**
	 * Float类型转换
	 */
	public static Float toFloat(String str, float defaultVal) {
		Float i = defaultVal;
		if (null != str && !"".equals(str)) {
			i = new Float(str);
		}
		return i;
	}

	/**
	 * Date类型转换
	 */
	public static Date toParseDate(String datetime) {
		/** */
		/**
		 * 字符串转换为java.util.Date 支持格式为: yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1
		 * AD at 22:10:59 PSD' yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00' yy/MM/dd
		 * HH:mm:ss pm 如 '2002/1/1 17:55:00 pm' yy-MM-dd HH:mm:ss 如 '2002-1-1
		 * 17:55:00' yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am'
		 * 
		 * @param time
		 *            String 字符串
		 * @return Date 日期
		 */
		String time = datetime;
		try{
			SimpleDateFormat formatter;

			int tempPos = time.indexOf("AD");
			time = time.trim();
			formatter = new SimpleDateFormat(" yyyy.MM.dd G 'at' hh:mm:ss z ");
			if (tempPos > -1) {
				time = time.substring(0, tempPos) + " 公元 "
						+ time.substring(tempPos + " AD ".length()); // china
				formatter = new SimpleDateFormat(
						" yyyy.MM.dd G 'at' hh:mm:ss z ");
			}

			tempPos = time.indexOf("-");

			if (time.indexOf('.') != -1) {
				time = time.substring(0, time.indexOf('.'));
			}

			if (tempPos > -1) {// 包含“-”
				if (time.indexOf(":") == -1) {
					formatter = new SimpleDateFormat("yyyy-MM-dd");
				} else if (time.indexOf(":") == time.lastIndexOf(":")) {
					formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				} else {
					formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				}
			} else if (time.indexOf("/") > -1) {// 包含“/”
				if (time.indexOf(":") == -1) {
					formatter = new SimpleDateFormat("yyyy/MM/dd");
				} else {
					formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				}
			} else if (time.substring(0, 8).indexOf("-") == -1) {
				time = time.substring(0, 8);
				formatter = new SimpleDateFormat("yyyyMMdd");
			} else {
				formatter = new SimpleDateFormat("HH:mm");
			}

			ParsePosition pos = new ParsePosition(0);
			Date ctime = formatter.parse(time, pos);
			return ctime;
		}catch (Exception ex) {
			return null;
		}

	}

	/**
	 * DataFormat
	 */
	public static String dataFormat(Date date, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		try {
			return df.format(date);
		} catch (Exception e) {
			return "";
		}

	}

	/**
	 * 获取当前日期第一天
	 */
	public static Date getFirstDayByMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int stDate = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-");
		String startDate1 = sdf.format(date).concat(String.valueOf(stDate));
		return toParseDate(startDate1);
	}

	/**
	 * 获取当前提前最后一天
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastDayByMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int enDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-");
		String endDate1 = sdf.format(date).concat(String.valueOf(enDate));
		return toParseDate(endDate1);
	}

	/**
	 * 获取当前日期下一天
	 * @throws ParseException 
	 */
	public static Date getNexDate(Date date) throws ParseException  {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(Calendar.DATE);
		calendar.set(Calendar.DATE, day + 1);
		Date dat = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(dat);
		return sdf.parse(date1);
	}

	/**
	 * 获取当前日期前一天
	 */
	public static Date getForwardOneDate(Date date) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int day = calendar.get(Calendar.DATE);
			calendar.set(Calendar.DATE, day - 1);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = sdf.format(dat);
			return sdf.parse(date1);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取当前日期前10天
	 */
	public static Date getForwardTenDate(Date date) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int day = calendar.get(Calendar.DATE);
			calendar.set(Calendar.DATE, day - 10);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = sdf.format(dat);
			return sdf.parse(date1);
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 * 获取当前日期前几天
	 */
	public static Date getForwardSumeDate(Date date, int days) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int day = calendar.get(Calendar.DATE);
			calendar.set(Calendar.DATE, day - days);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = sdf.format(dat);
			return sdf.parse(date1);	
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 * 获取当前日期后7天
	 */
	public static Date getSevenDate(Date date) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int day = calendar.get(Calendar.DATE);
			calendar.set(Calendar.DATE, day + 6);
			Date dat = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = sdf.format(dat);
			return sdf.parse(date1);
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 * 处理当前日期去掉小时分钟秒
	 * @throws ParseException 
	 */
	public static Date getCurrentDate(Date date) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = sdf.format(date);
		return sdf.parse(date1);		
	}

	 /**
     * 标准化时间显示
     * yyyy-MM-dd HH:mm:ss
     * @param dateStr
     * @return
     */
    public static String utc2Local(String dateStr) {
        String[] aStrings = dateStr.split(" ");
        // 5
        if ("Jan".equals(aStrings[1])) {
            aStrings[1] = "01";
        }
        if ("Feb".equals(aStrings[1])) {
            aStrings[1] = "02";
        }
        if ("Mar".equals(aStrings[1])) {
            aStrings[1] = "03";
        }
        if ("Apr".equals(aStrings[1])) {
            aStrings[1] = "04";
        }
        if ("May".equals(aStrings[1])) {
            aStrings[1] = "05";
        }
        if ("Jun".equals(aStrings[1])) {
            aStrings[1] = "06";
        }
        if ("Jul".equals(aStrings[1])) {
            aStrings[1] = "07";
        }
        if ("Aug".equals(aStrings[1])) {
            aStrings[1] = "08";
        }
        if ("Sep".equals(aStrings[1])) {
            aStrings[1] = "09";
        }
        if ("Oct".equals(aStrings[1])) {
            aStrings[1] = "10";
        }
        if ("Nov".equals(aStrings[1])) {
            aStrings[1] = "11";
        }
        if ("Dec".equals(aStrings[1])) {
            aStrings[1] = "12";
        }
        if(aStrings[2].length()<2){
        	aStrings[2]="0"+aStrings[2];
        }
        return aStrings[6] + "-" + aStrings[1] + "-" + aStrings[2] + " " + aStrings[3];
    }
}
